package br.ufal.ic.dao.mock;

import br.ufal.ic.core.Ranking;
import br.ufal.ic.dao.RankingDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Marcos Paulo
 */
public class MockRankingDAO implements RankingDAO {

    private Map<String, Ranking> rankings;

    public MockRankingDAO() {
        rankings = new HashMap<String, Ranking>();
    }

    public MockRankingDAO(Set<Ranking> rankings) {
        this();
        initRankings(rankings);

    }

    private void initRankings(Set<Ranking> rankings) {
        for (Ranking ranking : rankings) {
            save(ranking);
        }
    }

    public void save(Ranking ranking) {
        if (ranking == null) {
            throw new IllegalArgumentException("infome o ranking a ser salvo");
        }

        if (rankings.containsValue(ranking)) {
            throw new RuntimeException("ranking ja cadastrado.");
        }

        rankings.put(ranking.getId(), ranking);
    }

    public void delete(Ranking ranking) {
        if (rankings.remove(ranking.getId()) == null) {
            throw new RuntimeException("ranking não salvo : " + ranking);
        }
    }

    public void update(Ranking ranking) {
        delete(ranking);

        save(ranking);
    }

    public List<Ranking> findAll() {
        return new ArrayList<Ranking>(rankings.values());
    }

    public Ranking findById(String id) {
        return rankings.get(id);
    }
}
